package com.thm.tfast.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.enums.DeleteFlagEnum;
import com.thm.tfast.core.exception.Asserts;
import com.thm.tfast.core.sevruity.domain.SysUserDetails;
import com.thm.tfast.core.utils.SecurityUtil;
import com.thm.tfast.modules.sys.model.SysMenu;
import com.thm.tfast.modules.sys.model.SysOrganization;
import com.thm.tfast.modules.sys.mapper.SysOrganizationMapper;
import com.thm.tfast.modules.sys.model.SysUser;
import com.thm.tfast.modules.sys.model.dto.organization.OrganizationInsertDto;
import com.thm.tfast.modules.sys.model.dto.organization.OrganizationUpdateDto;
import com.thm.tfast.modules.sys.service.SysOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thm.tfast.modules.sys.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 组织架机信息 服务实现类
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@Service
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements SysOrganizationService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public Page<SysOrganization> page(String keyword, Long parentId, Integer pageNum, Integer pageSize) {
        Page<SysOrganization> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysOrganization> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(SysOrganization::getName, keyword);
        }
        if (Objects.nonNull(parentId)){
            wrapper.eq(SysOrganization::getParentId,parentId);
        }
        wrapper.ne(SysOrganization::getDelFlag,DeleteFlagEnum.DELETED.getCode());
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public Long insert(OrganizationInsertDto dto) {
        SysOrganization sysOrganization = this.getOne(Wrappers
                .<SysOrganization>lambdaQuery()
                .eq(SysOrganization::getName, dto.getName()));
        if (Objects.nonNull(sysOrganization)){
            Asserts.fail("组织机构已经存在");
        }
        sysOrganization = BeanUtil.toBean(dto, SysOrganization.class);
        if (Objects.nonNull(sysOrganization.getParentId())){
            SysOrganization parentOrganization = this.getById(sysOrganization.getParentId());
            if (Objects.isNull(parentOrganization)){
                Asserts.fail("父级组织机构不存在");
            }
            sysOrganization.setLevel(sysOrganization.getLevel() + 1);
        }
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysOrganization.setCreateUser(currentUser.getUserId());
        sysOrganization.setCreateTime(currentUser.getDate());
        sysOrganization.setUpdateUser(currentUser.getUserId());
        sysOrganization.setUpdateTime(currentUser.getDate());
        this.save(sysOrganization);
        return sysOrganization.getId();
    }

    @Override
    @Transactional
    public Boolean update(OrganizationUpdateDto dto) {
        if (Objects.isNull(this.getById(dto.getId()))){
            Asserts.fail("组织机构不存在");
        }
        SysOrganization sysOrganization = BeanUtil.toBean(dto, SysOrganization.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysOrganization.setUpdateUser(currentUser.getUserId());
        sysOrganization.setUpdateTime(currentUser.getDate());
        return this.updateById(sysOrganization);
    }

    @Override
    @Transactional
    public Boolean delete(Long orgId) {
        SysOrganization sysOrganization = this.getById(orgId);
        if (Objects.isNull(sysOrganization)){
            Asserts.fail("组织机构不存在");
        }
        List<SysOrganization> sysOrganizations = this.list(Wrappers
                .<SysOrganization>lambdaQuery()
                .eq(SysOrganization::getParentId, orgId));
        if (CollectionUtil.isNotEmpty(sysOrganizations)){
            Asserts.fail("当前组织机构存在子机构不允许删除");
        }
        List<SysUser> sysUsers = sysUserService.list(Wrappers
                .<SysUser>lambdaQuery()
                .eq(SysUser::getOrgId, orgId));
        if (CollectionUtil.isNotEmpty(sysUsers)){
            Asserts.fail("当前组织机构存在用户不允许删除");
        }
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysOrganization.setUpdateUser(currentUser.getUserId());
        sysOrganization.setUpdateTime(currentUser.getDate());
        sysOrganization.setDelFlag(DeleteFlagEnum.DELETED.getCode());
        return this.updateById(sysOrganization);
    }
}
